package com.okhttp.utils;

import com.okhttp.retrofit.RetrofitClient;

/**
 * @author YLG
 * @Datetime 2020/4/17-10:01 PM
 * @Version v1.0
 * @description
 */
public class RetrofitLog {
  
  private static class RfLogImpl implements IRetrofitLog {
    
    @Override
    public void d(String tag, String msg) {
      android.util.Log.d(tag, log(msg));
    }
    
    @Override
    public void e(String tag, String s) {
      android.util.Log.e(tag, log(s));
    }
    
    @Override
    public void e(String tag, String message, Throwable t) {
      android.util.Log.e(tag, log(message), t);
    }
    
    static String log(String prefix) {
      StackTraceElement caller = Thread.currentThread().getStackTrace()[4];
      String tag = "[%s.%s(L:%d)]: >>> %s";
      String callerClazzName = caller.getClassName();
      callerClazzName = callerClazzName.substring(callerClazzName.lastIndexOf(".") + 1);
      tag = String.format(tag, callerClazzName, caller.getMethodName(), caller.getLineNumber(), prefix == null ? "" : prefix);
      return tag;
    }
  }
  
  private static IRetrofitLog mIRetrofitLog = new RfLogImpl();
  
  public static void setIRetrofitLog(IRetrofitLog retrofitLog) {
    if (retrofitLog != null) {
      mIRetrofitLog = retrofitLog;
    }
  }
  
  public static void d(String tag, String msg) {
    if (RetrofitClient.getInstance().isDebug()) {
      mIRetrofitLog.d(tag, msg);
    }
  }
  
  public static void e(String tag, String s) {
    if (RetrofitClient.getInstance().isDebug()) {
      mIRetrofitLog.e(tag, s);
    }
  }
  
  public static void e(String tag, String message, Throwable t) {
    if (RetrofitClient.getInstance().isDebug()) {
      mIRetrofitLog.e(tag, message, t);
    }
  }
  
  public static void df(String tag, String formatMsg, Object... args) {
    if (RetrofitClient.getInstance().isDebug()) {
      mIRetrofitLog.d(tag, String.format(formatMsg, args));
    }
  }
}
